//
//  ZJTextView.swift
//  mini
//
//  Created by Mac on 2017/5/22.
//  Copyright © 2017年 Mac. All rights reserved.
//

import UIKit

class ZJTextView: UITextView {
    
    override  var text: String!{
        didSet{
           textChange()
        }
    }
    override var attributedText: NSAttributedString!{
        didSet{
            textChange()
        }
    }

    var label:UILabel?
    
    var placeholder:String?{
        didSet{
          label?.text = placeholder
          setNeedsLayout()
        }
    }
    var placeholderColor:UIColor?{
        didSet{
          label?.textColor = placeholderColor
            self.tintColor = placeholderColor
        }
    }
    var placeholderFont:UIFont?{
        didSet{
            label?.font = placeholderFont
            self.font  = placeholderFont
             setNeedsLayout()
        }
    }
    var myBorderColor:UIColor?{
        didSet{
            
            self.layer.borderColor = myBorderColor?.cgColor
            
        }
    }
    var myBorderWidth:CGFloat?{
        didSet{
            self.layer.borderWidth = myBorderWidth!
            
        }
    }
    var myBorderRradian:CGFloat?{
        didSet{
            self.layer.cornerRadius = myBorderRradian!
            self.layer.masksToBounds = true
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){

        label = UILabel()
        label?.backgroundColor = UIColor.white
        label?.numberOfLines = 0;
        addSubview(label!)
        
        placeholderColor = UIColor.lightGray //设置 颜色
        placeholderFont = UIFont.systemFont(ofSize: 14)  //设置字体大小
        font = placeholderFont
        myBorderColor  = kSeparateColor
        myBorderWidth = kSeperateLineWidth
        myBorderRradian = 0
    
        
        NotificationCenter.default.addObserver(self, selector: #selector(textChange), name:NSNotification.Name.UITextViewTextDidChange, object: self)
        
      
    }
    
    override func layoutSubviews() {
        
////        // 设置坐标
        let labelX:CGFloat = 5;
        let labelY:CGFloat = 8;
        let labelW:CGFloat = frame.size.width - labelX*2;
        // 根据文字计算高度
        let maxSize:CGSize = CGSize(width: labelW, height: CGFloat(MAXFLOAT))

        let _placeholder:NSString = placeholder! as NSString
        
        let option = NSStringDrawingOptions.usesLineFragmentOrigin
//        是数组了不是字典
        let textRect:CGRect = _placeholder.boundingRect(with: maxSize, options: option, attributes: [NSFontAttributeName:placeholderFont ?? UIFont.systemFont(ofSize: 14)], context: nil)
         let textSize:CGSize = textRect.size
        let labelH:CGFloat = textSize.height
        // 设置Frame
        label?.frame = CGRect(x: labelX, y: labelY, width: labelW, height: labelH)
//        label?.snp.makeConstraints({ (make) in
//            make.top.equalTo(self).offset(-20)
//            make.left.equalTo(self).offset(10)
//        })
        
    }
    
    func textChange()  {
          
        label?.isHidden = hasText
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self)
        
    }
    
    
}
